/*
 *  $Id: BlastedGround.cs 939 2009-12-03 13:12:41Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System.Collections.Generic;

namespace Ragima.Cards001.Fire.Spells {
    public class BlastedGround: TargetEnchantmentCard<Land> {
        public BlastedGround(): base("001_f_s_blasted_ground") {
            Level = CompleteLevel.ReadyForTest;
            Element = ElementType.Fire;
            Value = 100;
            Target = new LandTarget(GetTargetPhrase(), true, true, NotWaterAndManaEnough);
        }

        private static bool NotWaterAndManaEnough(TargetRequest request, Land land) {
            if (land.Card.Element == ElementType.Water) return false;
            if (land.HasAffect<BlastedGroundAffect>()) return false;
            int cur = request.Player.GetMana(ElementType.Fire);
            int req = land.Summary.GetManaIncomeAll();
            return req > 0 && cur >= req;
        }

        protected override void DoAction(TargetArgs<Land> args) {
            int mana = args.Target.Summary.GetManaIncomeAll();
            args.Player.DecrementMana(ElementType.Fire, mana);
            base.DoAction(args);
        }
        protected override EnchantmentAffect CreateEnchantment() {
            return new BlastedGroundAffect();
        }

        private class BlastedGroundAffect: EnchantmentAffect<BlastedGround> {
            public override void SetAffect() {
                base.SetAffect();
                foreach (Affects.ManaIncome mana in GetNaturalIncomes()) {
                    if (mana.SourceElement != ElementType.Fire) {
                        LocalSummary.ChangeManaIncome(mana.SourceElement, -mana.Value);
                        GlobalSummary.ChangeManaIncome(mana.SourceElement, -mana.Value);
                        LocalSummary.ChangeManaIncome(ElementType.Fire, mana.Value);
                        GlobalSummary.ChangeManaIncome(ElementType.Fire, mana.Value);
                    }
                }
            }
            public override void ResetAffect() {
                base.ResetAffect();
                foreach (Affects.ManaIncome mana in GetNaturalIncomes()) {
                    if (mana.SourceElement != ElementType.Fire) {
                        LocalSummary.ChangeManaIncome(mana.SourceElement, mana.Value);
                        GlobalSummary.ChangeManaIncome(mana.SourceElement, mana.Value);
                        LocalSummary.ChangeManaIncome(ElementType.Fire, -mana.Value);
                        GlobalSummary.ChangeManaIncome(ElementType.Fire, -mana.Value);
                    }
                }
            }

            private IEnumerable<Affects.ManaIncome> GetNaturalIncomes() {
                NaturalAffect natural = Owner.GetAffect<NaturalAffect>();
                if (natural != null) {
                    foreach (Affect affect in natural) {
                        if (affect is Affects.ManaIncome)
                            yield return (Affects.ManaIncome) affect;
                    }
                }
            }
        }
    }
}
